package org.siam.impl.schemas {
	
	import org.siam.core.ISiamManager;
	import org.siam.core.data.editors.IEditorContext;
	import org.siam.core.data.renderers.IRendererContext;
	import org.siam.core.data.validators.IValidatorContext;
	import org.siam.core.schemas.ISchema;
	import org.siam.core.schemas.ISchemaManager;
	import org.siam.core.schemas.properties.ISchemaProperty;

	public class SchemaManager implements ISchemaManager {
		
		private var _siamManager:ISiamManager;
		private var _schema:ISchema;
		
		public function SchemaManager(siamManager:ISiamManager, schema:ISchema) {
			_siamManager = siamManager;
			_schema = schema;
		}

		public function get propertyIds():Array {
			return _schema.propertyIds;
		}
		
		public function findMetadata(metaKey:String, propertyId:String=null):String {
			if (propertyId != null) {
				return findProperty(propertyId).findMetadata(metaKey);
			} else {
				return _schema.findMetadata(metaKey);
			}
		}
		
		public function format(model:Object, propertyId:String):String {
			var value:Object = model ? model[propertyId] : null;
			var formatterId:String = findProperty(propertyId).formatterId;
			return _siamManager.formatters.format(value, formatterId);
		}
		
		public function findRenderer(propertyId:String):IRendererContext {
			var rendererId:String = findProperty(propertyId).rendererId;
			return _siamManager.renderers.find(rendererId);
		}
		
		public function findEditor(propertyId:String):IEditorContext {
			var editorId:String = findProperty(propertyId).editorId;
			return _siamManager.editors.find(editorId);
		}
		
		public function findValidator(propertyId:String):IValidatorContext {
			var validatorId:String = findProperty(propertyId).validatorId;
			return _siamManager.validators.find(validatorId);
		}
		
		private function findProperty(id:String):ISchemaProperty {
			return _schema.findProperty(id);
		}
	}
}